package utils;

import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.text.html.FormSubmitEvent.MethodType;

import model.DATAError;
import model.DocumentEA;

public class ObjectUtils {

	public static boolean propsNotNull(Object obj) {
		boolean isNotNull = true;
		Class<?> cls = obj.getClass();
		for (Field fld : cls.getDeclaredFields()) {
			fld.setAccessible(true);
			if (Modifier.isPrivate(fld.getModifiers())) {
				try {
					if (fld.get(obj) == null) {
						isNotNull = false;
						break;
					}
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return isNotNull;
	}

	public static boolean propsNotNullandEmpty(Object obj) {
		boolean isNotNullandEmpty = true;
		Class<?> cls = obj.getClass();
		for (Field fld : cls.getDeclaredFields()) {
			fld.setAccessible(true);
			if (Modifier.isPrivate(fld.getModifiers())) {
				try {
					if (fld.get(obj) == null) {
						isNotNullandEmpty = false;
						break;

					}
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return isNotNullandEmpty;
	}

	public static boolean methodExist(Object obj, String methodName) {
		boolean result = false;
		Class<?> cls = obj.getClass();

		for (Method mtd : cls.getDeclaredMethods()) {
			if (mtd.getName().equals(methodName)) {
				result = true;
				break;
			}
		}

		return result;
	}

	public static boolean fieldExsist(Object obj, String fieldName) {
		boolean result = false;
		Class<?> cls = obj.getClass();

		for (Field fld : cls.getDeclaredFields()) {
			if (fld.getName().equals(fieldName)) {
				result = true;
				break;
			}
		}

		return result;
	}

	public static Object createClassByName(String caonicalClassName) {
		try {
			Class<?> clazz = Class.forName(caonicalClassName);
			return clazz.newInstance();
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException ex) {
			MessageHandler.getErrors().add(
					new DATAError("error", ex.getMessage()));
			MessageHandler.getMessageHandler().ShowErrors();
		}
		return caonicalClassName;
	}

	public static void setField(Object currClass, String fieldname, Object value) {
		Class<?> currcl = currClass.getClass();
		for (Field fld : currcl.getDeclaredFields()) {
			fld.setAccessible(true);
			System.out.println("Field name " + fld.getName());
			if (fld.getName().equals(fieldname)) {

				try {
					fld.set(currClass, value);
					System.out.println("Field Set!");
				} catch (IllegalArgumentException | IllegalAccessException e) {

					MessageHandler.showSingleError(e.getMessage());

				}
			}
		}
	}

	public static Object[] getNotNUllOfClass(Object currClass) {
		ArrayList<Object> objarr = new ArrayList<Object>();
		Class<?> currcl = currClass.getClass();
		for (Field fld : currcl.getDeclaredFields()) {
			fld.setAccessible(true);
			objarr.add(ObjectUtils.getValueByName(currClass, fld.getName()));

		}

		Object[] obj = objarr.toArray();
		return obj;

	}

	public static Object getValueByName(Object currClass, String fieldName) {

		Class<?> currcl = currClass.getClass();
		for (Field fld : currcl.getDeclaredFields()) {
			fld.setAccessible(true);
			if (fld.getName().equals(fieldName)) {
				try {
					return fld.get(currClass);
				} catch (IllegalArgumentException | IllegalAccessException e) {
					MessageHandler.showSingleError(e.getMessage());
				}
			}

		}

		return null;
	}

	public static void main(String args[]) {
		System.out.println("Object is not null & empty "
				+ ObjectUtils.propsNotNullandEmpty(new DocumentEA("thisname",
						"document", "1.0", "asdsad")));
	}

}
